package cn.tedu.charging.order.config;

import cn.tedu.charging.common.constant.AmqpComponentConst;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

@Configuration
public class AmqpDeclareConfiguration {
    //声明业务交换机(fanout)
    @Bean
    public Exchange delayBillEx(){
        return ExchangeBuilder.fanoutExchange(AmqpComponentConst.DELAY_BILL_EX).build();
    }
    //声明业务队列 最复杂
    @Bean
    public Queue delayBillQueue(){
        //队列名称 持久化 专属 自动删除 队列属性
        return QueueBuilder.nonDurable(AmqpComponentConst.DELAY_BILL_QUEUE)
                //消息最大存活时间 60秒
                .withArgument("x-message-ttl",1000*60)
                //绑定死信交换机
                .withArgument("x-dead-letter-exchange",AmqpComponentConst.DELAY_BILL_DL_EX)
                //绑定死信队列的路由键
                .withArgument("x-dead-letter-routing-key",AmqpComponentConst.DELAY_BILL_DL_RK)
                .build();
    }
    //声明业务交换机和业务队列的绑定关系
    @Bean
    public Binding delayBillExAndQueue(){
        return BindingBuilder.bind(delayBillQueue()).to(delayBillEx()).with("").noargs();
    }
    //声明死信交换机(direct)
    @Bean
    public Exchange delayBillDlEx(){
        return ExchangeBuilder.directExchange(AmqpComponentConst.DELAY_BILL_DL_EX).build();
    }
    //声明死信队列
    @Bean
    public Queue delayBillDlQueue(){
        return QueueBuilder.nonDurable(AmqpComponentConst.DELAY_BILL_DL_QUEUE).build();
    }
    //声明死信交换机和死信队列的绑定关系
    @Bean
    public Binding delayBillDlExAndQueue(){
        return BindingBuilder.bind(delayBillDlQueue()).to(delayBillDlEx())
                .with(AmqpComponentConst.DELAY_BILL_DL_RK).noargs();
    }
}
